package com.hms.service.impl;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import com.hms.dao.IAdminDao;
import com.hms.dao.IBaseDao;
import com.hms.entity.Admin;
import com.hms.service.IAdminService;

@Service("adminService")
public class AdminService extends BaseService<Admin, String> implements IAdminService {

	private static Logger logger = Logger.getLogger(AdminService.class);

	@Resource
	private IAdminDao adminDao;

	@Override
	public boolean isExistByUserName(String userName) {
		Admin admin = super.get("username", userName);
		return admin == null ? false : true;
	}

	@Override
	public Admin getAdminByuUserName(String userName) {
		return super.get("username", userName);
	}

	@Override
	public Admin getLoginAdmin() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (null == authentication) {
			logger.debug(" 当前没有登录用户！！！");
			return null;
		}
		Object principal = authentication.getPrincipal();
		if (null != principal && principal instanceof Admin) {
			return (Admin) principal;
		}
		logger.debug("当前登录用户非法！！！");
		return null;
	}

	@Override
	public Admin loadLoginAdmin() {
		Admin admin = getLoginAdmin();
		if (admin == null) {
			return null;
		}
		return this.adminDao.load(admin.getId());
	}

	@Override
	public IBaseDao<Admin, String> getDao() {
		return adminDao;
	}

}
